package com.example.service.impl.pc;

import com.example.entity.dto.pc.ListDto;
import com.example.entity.vo.hzView.HzInvmbView;
import com.example.entity.vo.pc.ItemVo;
import com.example.entity.vo.pc.ListVo;
import com.example.entity.vo.pc.V_WLZXQ;
import com.example.entity.ymlConfig.YmlConfig;
import com.example.mapper.hzViewSync.HzViewSyncMapper;
import com.example.mapper.pc.WLPHZBMapper;
import com.example.service.pc.WLPHZBService;
import com.example.untils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class WLPHZBServiceImpl implements WLPHZBService {

    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Autowired
    private HzViewSyncMapper hzViewSyncMapper;
    @Resource
    private WLPHZBMapper mapper;

    @Transactional
    @Override
    public Boolean refreshWLXQ() {
        String sql = "EXEC sp_wlxq;";
        jdbcTemplate.execute(sql);
        return true;
    }

    @Override
    public Boolean refreshWLXQhzView() {
//        System.out.println("PPPPPPPPPPPPPPPPPPPQQQQQQQQQQQQQQQQQQQ");
        String sql = "EXEC sp_wlxq;";
        jdbcTemplate.execute(sql);
        return true;
    }

    //    @Async
    @Override
    public ListVo list(ListDto dto) {
        if(StringUtils.isNotEmpty(dto.getProductSpecifications())) {
            dto.setProductSpecifications(dto.getProductSpecifications().replaceAll("([\\[\\]*+_%/^])", "/$1"));
        }
        ListVo res = new ListVo();
        List<ItemVo> list = null;
        if("hz".equals(YmlConfig.getActive()) || "hznw".equals(YmlConfig.getActive())) {
            list = mapper.listHzFaster(dto);
        }else{
            list = mapper.list(dto);
        }

        // 优化的代码尝试 start----
        List<HzInvmbView> viewList = hzViewSyncMapper.hzMB001MD003byINVMBViewList();
        Map<String, Double> wlphzbMap = new HashMap<>();
        for(HzInvmbView t : viewList) wlphzbMap.put(t.getMB001().trim(), new Double(t.getMD003()));
        for(ItemVo item : list) {
            Double aDouble = wlphzbMap.get(item.getProductNo().trim());
            if(aDouble == null) aDouble = new Double(0D);
            item.setPurchasedNoReturnNums(item.getPurchasedNoReturnNums() * aDouble);
            item.setMaterialBalanceStatus(item.getMaterialBalanceStatus() + item.getPurchasedNoReturnNums());
        }
        // END ------


        List<String> dates = mapper.V_XQRQ(dto.getTimeStart(), dto.getTimeEnd());
        List<V_WLZXQ> WLZXQlist = mapper.V_WLZXQ(dto.getTimeStart(), dto.getTimeEnd());

        Map<String, Integer> map = new HashMap<>();
        for(int i = 0; i < list.size(); i++) {
           map.put(list.get(i).getProductNo().trim() ,i);
        }
        for(ItemVo item : list) {   // 填充日期里面的数字为0
            List t = new ArrayList();
            for(String date : dates) {
                V_WLZXQ o = new V_WLZXQ();
                o.setXQRQ(date); o.setXQL(0D);
                t.add(o);
            }
            item.setList(t);
        }
        for(V_WLZXQ item : WLZXQlist) {   // 把值set 到日期进去
            Integer i = map.get(item.getID().trim());
            if(i == null) continue;
            List<V_WLZXQ> list1 = list.get(i).getList();
            for(V_WLZXQ date : list1) {
                if(date.getXQRQ().trim().equals(item.getXQRQ().trim())) {
                    date.setXQL(item.getXQL());
                    break;
                }
            }
        }
        for(ItemVo item : list) {   // 填充颜色
            if(item.getMaterialBalanceStatus() < 0) {
                item.setColor("red");
            }
            double sum = 0;
            for(V_WLZXQ v : item.getList()) {
                if(v.getXQL() == 0) continue;
                if(sum + v.getXQL() > item.getTotalInventoryNums()) {
                    v.setColor("yellow");
                }
                sum += v.getXQL();
            }
        }
        res.setList(list);
//        res.setDates(dates);
//        res.setNums(WLZXQlist);
        return res;
    }
}
